<?php
require_once ("../src/db/connectdatabase.php");

function getUuDai($idbank, $address, $typeuudai, $typeob, $numstart, $numrecord) {
	$con = new ConnectDB();
	
	$sql = "SELECT iduudai, nameproduct, image, infoview, saleoff, ngaybatdau, ngayhethan, loaithe, bankid, bankname, banklogo FROM uudai WHERE typeuudai LIKE '%".$typeuudai."%' AND bankid LIKE '%".$idbank."%' AND diachilienhe LIKE '%".$address."%'";
	if($typeob == 1) {// order by giam gia
		$sql .= " ORDER BY saleoff";
	} else if($typeob == 2) {// moi nhat
		$sql .= " ORDER BY datecreate DESC";
	} else if($typeob == 3) { // sap het hang
		$sql .= " ORDER BY ngayhethan DESC";
	} else {
		$sql .= " ORDER BY saleoff";
	}
	
	$sql .= " LIMIT ".$numstart.",".$numrecord;
	
	$arr = $con -> getvalueString($sql);
	
	for($i = 0; $i < count($arr); $i++) {
		$arr[$i][5] = int_to_date($arr[$i][5]);
		$arr[$i][6] = int_to_date($arr[$i][6]);
	}
	
	return $arr;
}

function int_to_date($int)
{
    $time  = date("d/m/Y", $int);
    return $time;
}


if(isset($_POST['idbre']) && isset($_POST['addre']) && isset($_POST['typrore']) && isset($_POST['typesort']) && isset($_POST['pagere'])) {
	$idbank = $_POST['idbre'];
	$address = $_POST['addre'];
	$typeuudai = $_POST['typrore'];
	$typesort = $_POST['typesort'];
	$page = $_POST['pagere'];
	
	$numberload = 10;
	
	$arr = getUuDai($idbank, $address, $typeuudai, $typesort, (($page - 1) * $numberload), $numberload);
	
	if(count($arr) > 0) {
		echo json_encode($arr);
	} else {
		echo json_encode('noload');
	}
}
//echo json_encode("asda");
?>